package com.rui.study.micro.shoppingbag.client;

import feign.*;
import feign.hystrix.HystrixFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@FeignClient(name = "com.rui.study.micro.attach", configuration = AttachClient.AttachClientConfiguration.class,
        fallback = AttachClient.AttachClientImpl.class)
public interface AttachClient {

    @RequestLine("GET /download?filename={filename}")
    Response getInputStream(@Param("filename") String filename);

    @RequestLine("GET /hello")
    String sayHello();

    @RequestLine("POST /hello")
    @Headers({"Content-Type: application/json;charset=utf-8"})
    @Body("%7B\"name\": \"{name}\"%7D")
    String sayHello(@Param("name") String name);

    @RequestLine("POST /api/svr")
    @Headers({"Content-Type: text/plain;charset=utf-8"})
    @Body("{appName}")
    String fetchApplication(@Param("appName") String appName);


    @Slf4j
    @Component
    class AttachClientImpl implements AttachClient {

        @Override
        public Response getInputStream(String filename) {
            log.error("服务降级...");
            return null;
        }

        @Override
            public String sayHello() {
            log.warn("服务降级...");
            return "Hi~";
        }

        @Override
        public String sayHello(String name) {
            log.warn("服务降级...");
            return "Hi~";
        }

        @Override
        public String fetchApplication(String filename) {
            log.warn("服务降级...");
            return null;
        }
    }

    @Configuration
    class AttachClientConfiguration {
    }
}
